<template>
  <div class="dashboard">
    <el-row :gutter="20" class="stats-row">
      <el-col :span="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-content">
            <div class="stats-icon" style="background: #1890ff20;">
              <el-icon :size="32" color="#1890ff"><Monitor /></el-icon>
            </div>
            <div class="stats-info">
              <div class="stats-value">{{ stats.total_devices }}</div>
              <div class="stats-label">设备总数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-content">
            <div class="stats-icon" style="background: #52c41a20;">
              <el-icon :size="32" color="#52c41a"><CircleCheck /></el-icon>
            </div>
            <div class="stats-info">
              <div class="stats-value">{{ stats.active_devices }}</div>
              <div class="stats-label">活动设备</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-content">
            <div class="stats-icon" style="background: #faad1420;">
              <el-icon :size="32" color="#faad14"><Document /></el-icon>
            </div>
            <div class="stats-info">
              <div class="stats-value">{{ stats.total_backups }}</div>
              <div class="stats-label">备份总数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-content">
            <div class="stats-icon" style="background: #722ed120;">
              <el-icon :size="32" color="#722ed1"><TrendCharts /></el-icon>
            </div>
            <div class="stats-info">
              <div class="stats-value">{{ stats.success_rate }}%</div>
              <div class="stats-label">成功率</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :span="12">
        <el-card class="info-card">
          <template #header>
            <div class="card-header">
              <span>最近备份</span>
              <el-button type="primary" link @click="gotoBackups">查看全部</el-button>
            </div>
          </template>
          <el-table :data="recentBackups" style="width: 100%" v-loading="loading">
            <el-table-column prop="device.name" label="设备名称" width="150" />
            <el-table-column label="备份时间" width="180">
              <template #default="{ row }">
                {{ formatTime(row.backup_time) }}
              </template>
            </el-table-column>
            <el-table-column label="状态" width="100">
              <template #default="{ row }">
                <el-tag :type="row.status === 'success' ? 'success' : 'danger'" size="small">
                  {{ row.status === 'success' ? '成功' : '失败' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="大小">
              <template #default="{ row }">
                {{ formatSize(row.file_size) }}
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>

      <el-col :span="12">
        <el-card class="info-card">
          <template #header>
            <div class="card-header">
              <span>设备列表</span>
              <el-button type="primary" link @click="gotoDevices">查看全部</el-button>
            </div>
          </template>
          <el-table :data="devices" style="width: 100%" v-loading="loading">
            <el-table-column prop="name" label="设备名称" width="150" />
            <el-table-column prop="ip_address" label="IP地址" width="150" />
            <el-table-column label="类型">
              <template #default="{ row }">
                {{ formatDeviceType(row.device_type) }}
              </template>
            </el-table-column>
            <el-table-column label="状态" width="100">
              <template #default="{ row }">
                <el-tag :type="row.is_active ? 'success' : 'info'" size="small">
                  {{ row.is_active ? '启用' : '禁用' }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { statsApi, backupApi, deviceApi } from '@/api'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const stats = ref({
  total_devices: 0,
  active_devices: 0,
  total_backups: 0,
  today_backups: 0,
  success_rate: 0,
  total_backup_size: 0
})
const recentBackups = ref([])
const devices = ref([])

const loadData = async () => {
  loading.value = true
  try {
    const [statsData, backupsData, devicesData] = await Promise.all([
      statsApi.getStatistics(),
      backupApi.getBackups({ limit: 5 }),
      deviceApi.getDevices({ limit: 5 })
    ])
    stats.value = statsData
    recentBackups.value = backupsData
    devices.value = devicesData
  } catch (error) {
    ElMessage.error('加载数据失败')
  } finally {
    loading.value = false
  }
}

const formatTime = (time) => {
  return new Date(time).toLocaleString('zh-CN')
}

const formatSize = (bytes) => {
  if (bytes < 1024) return bytes + ' B'
  if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB'
  return (bytes / 1024 / 1024).toFixed(2) + ' MB'
}

const formatDeviceType = (type) => {
  const typeMap = {
    'cisco_ios': 'Cisco IOS',
    'cisco_xe': 'Cisco XE',
    'huawei': '华为',
    'hp_comware': 'H3C'
  }
  return typeMap[type] || type
}

const gotoBackups = () => router.push('/backups')
const gotoDevices = () => router.push('/devices')

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.dashboard {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.stats-row {
  margin-bottom: 0;
}

.stats-card {
  cursor: pointer;
  transition: transform 0.3s;
}

.stats-card:hover {
  transform: translateY(-4px);
}

.stats-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.stats-icon {
  width: 60px;
  height: 60px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stats-info {
  flex: 1;
}

.stats-value {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  line-height: 1;
  margin-bottom: 8px;
}

.stats-label {
  font-size: 14px;
  color: #666;
}

.info-card {
  height: 400px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 500;
}
</style>

